package com.wangjw.tuling.nio.buffers;

import java.nio.ByteBuffer;

/**
 * Created by wangjianwen on 2018/12/4.
 */
public class BufferCreate {

    public static void main(String[] args) {
        ByteBuffer byteBuffer0 = ByteBuffer.allocate(10);
        printBufferInfo(byteBuffer0);

        ByteBuffer byteBuffer1 = ByteBuffer.allocateDirect(10);
        printBufferInfo(byteBuffer1);

        byte[] bytes = new byte[10];
        ByteBuffer byteBuffer2 = ByteBuffer.wrap(bytes);
        printBufferInfo(byteBuffer2);

        byte[] bytes1 = new byte[10];
        ByteBuffer byteBuffer3 = ByteBuffer.wrap(bytes1, 2, 3);
        printBufferInfo(byteBuffer3);
    }

    private static void printBufferInfo(ByteBuffer byteBuffer) {
        if (byteBuffer.hasArray()) {
            System.out.println("byteBuffer array: " + byteBuffer.array());
            System.out.println("byteBuffer array offset: " + byteBuffer.arrayOffset());
        }

        System.out.println("Capacity: " + byteBuffer.capacity());
        System.out.println("Limit: " + byteBuffer.limit());
        System.out.println("Position: " + byteBuffer.position());
        System.out.println("Remaining: " + byteBuffer.remaining());
        System.out.println("Array: " + byteBuffer.toString());
        System.out.println();
    }
}
